<template>
  <div>
    <el-menu :default-active="activeIndex" class="el-menu-demo table-class" mode="horizontal" @select="handleSelect">
      <el-menu-item index="1" class="table-item-class">首页</el-menu-item>
      <el-menu-item index="2" class="table-item-class">比赛</el-menu-item>
      <el-menu-item index="3" class="table-item-class">题库</el-menu-item>
      <el-menu-item index="4" class="table-item-class">提交</el-menu-item>
      <el-menu-item index="5" class="table-item-class">预测</el-menu-item>
      <el-menu-item index="6" class="table-item-class">推荐</el-menu-item>
      <div class="right-icon">
        <img :src="userInfo.icon?userInfo.icon:defaultIcon" style="border-radius: 50%;width: 35px"/>
        <el-dropdown>
          <span>
           <span>{{userInfo.username}}</span>
           <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="logout">注销</el-dropdown-item>
            <el-dropdown-item @click.native="updateUserInfo">修改信息</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-menu>
    <div class="line"></div>
    <router-view></router-view>
  </div>
</template>

<script>
  import Api from '../common/api';

  export default {
    name: "index",
    components:{
    },
    data() {
      return {
        activeIndexPage: [
          '/index/firstIndex',
          '/index/matchPage',
          '/index/questionBank',
          '/index/submitPage',
          '/index/forecastPage',
          '/index/recommendPage'
        ],
        activeIndex: '1',
        active:'',
        userInfo: '',
        defaultIcon: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2908207382,1429820199&fm=26&gp=0.jpg'
      }
    },
    beforeRouteEnter(to, from, next) {
      next(vm => {
        // console.log(vm);//当前组件的实例
        let user = Api.getStorageItem('user');
        if (!user) {
          vm.$router.push('/login');
        } else if (user === '过期') {
          localStorage.removeItem('user');
          vm.$message.error('您的登录已经过期，请重新登录');
          vm.$router.push('/login');
        } else {
          vm.userInfo = user;
        }
      });
    },
    methods: {
      handleSelect(key, keyPath) {
        if (key ==='1'){
          this.$router.push(this.activeIndexPage[0])
        }
        if (key ==='2'){
          this.$router.push(this.activeIndexPage[1])
        }
        if (key ==='3'){
          this.$router.push(this.activeIndexPage[2])
        }
        if (key ==='4'){
          this.$router.push(this.activeIndexPage[3])
        }
        if (key ==='5'){
          this.$router.push(this.activeIndexPage[4])
        }
        if (key ==='6'){
          this.$router.push(this.activeIndexPage[5])
        }
        console.log(key, keyPath);
      },
      toCheckFilled(){
        this.$router.push('/index/checkFilled')
      },
      toBackUser(){
        this.$router.push('/index/backUser')
      },
      toMobileUser(){
        this.$router.push('/index/mobileUser')
      },
      changeActive(data){
        console.log('已经提交的data数据',data);
        this.active=data;
      },
      updateUserInfo(){
        this.$router.push('/index/updateInfo')
      },
      toAllBook(){
        this.$router.push('/index/allBook')
      },
      toMyBook(){
        this.$router.push('/index/myBook');
      },
      toAddBook(){
        this.$router.push('/index/addBook')
      },
      addPaper(){
        this.$router.push('/index/addPaper')
      },
      myPaper(){
        this.$router.push('/index/myPaper')
      },
      logout(){
        console.log('退出');
        localStorage.removeItem("user");
        Api.setStorageItem();
        this.$router.push('/login')
      },
      searchMusic(){
        this.$router.push('/index/musicSearch')
      },
      mineMusic(){
        this.$router.push('/index/musicMine')
      }
    }
  }
</script>

<style scoped>
  .table-item-class{
    color: #ffffff;
    width: 7%;
  }
  .table-class{
    background-color: #349BFA;
    color: #ffffff;
  }
  .item-class{
    padding-right: 60px;
    padding-left: 0;
  }
  .span-class{
    width: 100%;
  }
  .item-group-class{
    margin-left: 0;
  }
  .el-dropdown{
    margin: auto 5px;
    color: #FFFFFF;
  }
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
  .right-icon {
    float: right;
    display: flex;
    font-size: 20px;
    padding-right: 20px;
    margin-top: 10px;
  }

  .el-header {
    background-color: #363D3F;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .el-aside {
    background-color: #313743;
  }

  .el-main {
    background-color: #E9EDF0;
  }

  .container-class {
    height: 100%;
  }
</style>
